﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using System.Collections;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmQuanly_PHONG : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }
        Double temp = 1;
        PhongBUS busPhong;
        LoaiphongBUS busLoaiphong = new LoaiphongBUS();
        public PhongDTO dtoPhong;
        ArrayList dsPhong;
        BindingSource bsData = null;
        public int iCo = 0;

        public int btClick = 0;

        public bool bNhapDung = false;
        public bool kt = false;

        private static string maPhong=""; // nhan du lieu MAKH tu form Tra cuu
        public string MAPHONG
        {
            get { return maPhong; }
            set { maPhong = value; }
        }
        public frmQuanly_PHONG()
        {
            InitializeComponent();
        }

        private void frmQuanly_PHONG_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            LayDanhSachPhong();
            LoadCombobox();
            Cbm_Trangthai.SelectedIndex = 0;

            DieukienMo(false);

            if (maPhong != "")
            {
                btnOpentextbox_Click(sender, e);
                Txt_Sophong.Text = maPhong;
                Btn_Tracuu_Click(sender, e);
            }
        }

        public void LamMoi()
        {
            Txt_Sophong.Text = "";
            Txt_Sokhach.Text = "";
            Cbm_Trangthai.SelectedIndex = 0;
            Cbm_Loaiphong.SelectedIndex = 0;
            Txt_Mota.Text = "";
        }

        public void DieukienMo(bool Mo)
        {
            Txt_Sophong.Enabled = Mo;
            Txt_Sokhach.Enabled = Mo;
            Cbm_Trangthai.Enabled = Mo;
            Cbm_Loaiphong.Enabled = Mo;
            Txt_Mota.Enabled = Mo;
            Gru_LKL.Enabled = Mo;
            Gru_TXS.Enabled = !Mo;
        }

        public void LoadCombobox()
        {
            DataTable tp = new DataTable();
            tp = busLoaiphong.SelectAll();
            Cbm_Loaiphong.DisplayMember = "TenLP";
            Cbm_Loaiphong.ValueMember = "TenLP";
            Cbm_Loaiphong.DataSource = tp;
        }

        public bool KiemTraSoPhong(string Ma)
        {
            dtoPhong = new PhongDTO();
            if (Ma == dtoPhong.SOPHONG)
                return true;
            return false;
        }

        private void LayDanhSachPhong()
        {
            busPhong = new PhongBUS();
            dsPhong = busPhong.LayDanhSachPhong();
            XuatDanhSachPhong(dsPhong);
            GridDS_Phong.CellContentClick += new DataGridViewCellEventHandler(GridDS_Phong_CellContentClick);

        }

        private void XuatDanhSachPhong(ArrayList dsPhong)
        {
            GridDS_Phong.AutoGenerateColumns = false;

            this.bsData = new BindingSource(dsPhong, "");
            GridDS_Phong.DataSource = bsData;
        }

        private void GridDS_Phong_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridDS_Phong.SelectedCells.Count > 0)
            {
                int row = GridDS_Phong.SelectedCells[0].RowIndex;
                Txt_Sophong.Text = GridDS_Phong.Rows[row].Cells[1].Value.ToString();
                Txt_Sokhach.Text = GridDS_Phong.Rows[row].Cells[2].Value.ToString();
                Cbm_Trangthai.Text = GridDS_Phong.Rows[row].Cells[3].Value.ToString();
                Cbm_Loaiphong.Text = GridDS_Phong.Rows[row].Cells[4].Value.ToString();
                Txt_Mota.Text = GridDS_Phong.Rows[row].Cells[5].Value.ToString();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
            {
                timer1.Enabled = false;
            }
        }

        private void Btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private int LayPhongDuocChon(out ArrayList dsPhong)
        {
            int iIndex = 0;
            dsPhong = new ArrayList();
            foreach (DataGridViewRow dong in this.GridDS_Phong.Rows)
            {
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dong.Cells[0];
                bool bChecked = (null != cell && null != cell.Value && true == (bool)cell.Value);
                if (bChecked)
                {
                    dsPhong.Add(dong.DataBoundItem);
                    iIndex = dong.Index;
                }
            }

            return iIndex;
        }

        private void XoaNhanVienDuocChonKhoiDanhSach()
        {
            for (int i = 0; i < GridDS_Phong.Rows.Count; i++)
            {
                DataGridViewRow dong = GridDS_Phong.Rows[i];
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dong.Cells[0];
                PhongDTO dtoPhongBiXoa = (PhongDTO)GridDS_Phong.Rows[cell.RowIndex].DataBoundItem;

                bool bChecked = (null != cell && null != cell.Value && true == (bool)cell.Value);
                if (bChecked)
                {
                    this.bsData.RemoveAt(cell.RowIndex);
                    i--;
                }
            }
        }

        private void XoaPhong()
        {
            ArrayList dsPhong;
            LayPhongDuocChon(out dsPhong);
            if (dsPhong.Count > 0)
            {
                PhongBUS busPhong = new PhongBUS();
                busPhong.Xoa(dsPhong);
                XoaNhanVienDuocChonKhoiDanhSach();
            }
            else
            {
                MessageBox.Show("Bạn phải chọn một phòng!");
                return;
            }
        }


        private bool KiemTraPhongCoChua()
        {
            PhongDTO dto = new PhongDTO();
            dto.SOPHONG = Txt_Sophong.Text;
            return busPhong.KTMA(dto);
        }

        private bool KiemTraDuLieuNhap()
        {
            if (Txt_Sokhach.Text == "" || Txt_Mota.Text == "" || Txt_Sokhach.Text == "")
            {
                MessageBox.Show("Bạn phải điền đầy đủ dữ liệu!");
                return false;
            }
            if (Txt_Sophong.Text.Trim() == "" || int.Parse(Txt_Sokhach.Text) < 0 || Cbm_Trangthai.SelectedItem.ToString().Trim() == ""
                || Cbm_Loaiphong.SelectedValue.ToString().Trim() == "" || Txt_Mota.Text.Trim() == "")
            {
                MessageBox.Show("Hãy xem lại dữ liệu nhập!");
                return false;
            }
            return true;
        }

        private void Btn_Them_Click(object sender, EventArgs e)
        {
            DieukienMo(true);
            Txt_Sophong.Focus();
            LamMoi();
            kt = true;
        }

        private void Btn_Xoa_Click(object sender, EventArgs e)
        {
            ArrayList dsPhong;
            LayPhongDuocChon(out dsPhong);
            if (dsPhong.Count > 0)
            {
                DialogResult kq = MessageBox.Show("Bạn có thật sự muốn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (kq == DialogResult.Yes)
                {
                    XoaPhong();
                    iCo = 1;
                    LamMoi();
                }
            }
            else
            {
                MessageBox.Show("Hãy chon một phòng muốn xóa!");
            }

        }


        private void Btn_Sua_Click(object sender, EventArgs e)
        {
            if (Txt_Sophong.Text!="" && Txt_Sokhach.Text!="")
            {
                DieukienMo(true);
                Txt_Sophong.Enabled = false;
                kt = false;
            }
            else
            {
                MessageBox.Show("Xin hãy chọn phòng mà bạn muốn sửa thông tin!");
            }
        }

        private void Btn_Tracuu_Click(object sender, EventArgs e)
        {
            if (Txt_Sophong.Enabled == false && Txt_Sokhach.Enabled == false)
            {
                MessageBox.Show("Xin hãy nhấn nút mở ô nhập liệu\nđể tiến hành nhập liệu và tra cứu","Thông báo tra cứu");
                btnOpentextbox.Focus();
                return;
            }

            PhongDTO dtoPhongCanTraCuu = new PhongDTO();
            dtoPhongCanTraCuu.TENLP = "";
            dtoPhongCanTraCuu.MOTA = "";
            dtoPhongCanTraCuu.TRANGTHAI = "";

            if (Txt_Sophong.Text == "")
                dtoPhongCanTraCuu.SOPHONG = "";
            else
                dtoPhongCanTraCuu.SOPHONG = Txt_Sophong.Text.ToString();

            if (Txt_Sokhach.Text == "")
                dtoPhongCanTraCuu.SOKHACHTOIDA = 0;
            else
                dtoPhongCanTraCuu.SOKHACHTOIDA = int.Parse(Txt_Sokhach.Text.ToString());

            ArrayList dsTraCuuDuoc = busPhong.TraCuuPhong(dsPhong, dtoPhongCanTraCuu);
            XuatDanhSachPhong(dsTraCuuDuoc);
            DieukienMo(false);
            LamMoi();
            if (GridDS_Phong.RowCount == 0)
            {
                MessageBox.Show("Không tìm thấy thông tin do bạn nhập\nXin vui lòng nhập lại thông tin khác","Thông báo tra cứu");
            }
        }

        private void Btn_LamMoi_Click(object sender, EventArgs e)
        {
            frmQuanly_LOAIPHONG frm = new frmQuanly_LOAIPHONG();
            frm.ShowDialog();
        }

        private void Btn_Load_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (kt == true)
                {
                    dtoPhong = new PhongDTO();
                    bNhapDung = KiemTraDuLieuNhap();
                    if (bNhapDung)
                    {
                        if (KiemTraPhongCoChua())
                        {
                            MessageBox.Show("Số phòng này đã tồn tại, xin nhập số phòng khác!");
                            Txt_Sophong.Focus();
                            Txt_Sophong.SelectAll();
                            return;
                        }

                        dtoPhong.SOPHONG = Txt_Sophong.Text.ToString();
                        dtoPhong.SOKHACHTOIDA = int.Parse(Txt_Sokhach.Text.ToString());
                        dtoPhong.TRANGTHAI = Cbm_Trangthai.SelectedItem.ToString();
                        dtoPhong.MOTA = Txt_Mota.Text.ToString();
                        dtoPhong.TENLP = Cbm_Loaiphong.SelectedValue.ToString();
                    
                        if(!busPhong.Them(dtoPhong))
                        {
                            MessageBox.Show("Thất bại! Xin hãy kiểm tra lại");
                            return;
                        }
                        if (bsData.Count == 0)
                        {
                            ArrayList dsPhong = new ArrayList();
                            dsPhong.Add(dtoPhong);
                            bsData = new BindingSource(dsPhong, "");
                            this.GridDS_Phong.DataSource = bsData;
                        }
                        else
                        {
                            bsData.Add(dtoPhong);
                        }
                    }
                }
                else 
                {
                    dtoPhong = new PhongDTO();
                    dtoPhong.SOPHONG = Txt_Sophong.Text.ToString();
                    dtoPhong.SOKHACHTOIDA = int.Parse(Txt_Sokhach.Text.ToString());
                    dtoPhong.TRANGTHAI = Cbm_Trangthai.SelectedItem.ToString();
                    dtoPhong.MOTA = Txt_Mota.Text.ToString();
                    dtoPhong.TENLP = Cbm_Loaiphong.SelectedValue.ToString();

                    if(!busPhong.CapNhatThongTin(dtoPhong))
                    {
                        MessageBox.Show("Thất bại! Xin hãy kiểm tra lại");
                        return;
                    }
                    ArrayList dsPhong = new ArrayList();
                    dsPhong.Add(dtoPhong);
                    bsData = new BindingSource(dsPhong, "");
                    this.GridDS_Phong.DataSource = bsData;
                }
            }
            catch
            {
                MessageBox.Show("Xin hãy nhập đầy đủ thông tin");
            }
            DieukienMo(false);
            LamMoi();
            Btn_Them.Focus();
        }

        private void Btn_Khongluu_Click(object sender, EventArgs e)
        {
            LamMoi();
            DieukienMo(false);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            LayDanhSachPhong();
        }

        private void Txt_Sokhach_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void btnOpentextbox_Click(object sender, EventArgs e)
        {
            LamMoi();
            DieukienMo(false);
            Gru_TXS.Enabled = false;
            Txt_Sophong.Enabled = true;
            Txt_Sokhach.Enabled = true;
            Txt_Sophong.Focus();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
